const { defineConfig } = require("@vue/cli-service");
const CompressionPlugin = require("compression-webpack-plugin");
// vue.config.js
const isProduction = process.env.NODE_ENV === "production";
const cdn = {
  css: [],
  js: [
    "https://cdn.bootcss.com/vue/2.6.14/vue.runtime.min.js",
    "https://cdn.bootcss.com/vue-router/3.5.1/vue-router.min.js",
    "https://cdn.bootcss.com/vuex/3.6.2/vuex.min.js",
    "https://cdn.bootcss.com/axios/0.27.2/axios.min.js",
  ],
};
const timeStamp = new Date().getTime();
module.exports = defineConfig({
  // publicPath: "/",
  productionSourceMap: false,
  transpileDependencies: true,
  chainWebpack: (config) => {
    /* 添加分析工具 */
    if (isProduction) {
      config
        .plugin("webpack-bundle-analyzer")
        .use(require("webpack-bundle-analyzer").BundleAnalyzerPlugin)
        .end();
      config.plugins.delete("prefetch"); // 生产环境注入cdn
      config.plugin("html").tap((args) => {
        args[0].cdn = cdn;
        return args;
      });
    } else {
    }
  },
  configureWebpack: (config) => {
    if (isProduction) {
      // 用cdn方式引入
      config.externals = {
        vue: "Vue",
        vuex: "Vuex",
        "vue-router": "VueRouter",
        axios: "axios",
      };
      config.mode = "production";
      return {
        plugins: [
          new CompressionPlugin({
            test: /\.js$|\.html$|\.css/, //匹配文件名
            threshold: 10240, //对超过10k的数据进行压缩
            deleteOriginalAssets: false, //是否删除原文件
          }),
        ],
      };
    }
  },
});
